package com.mt.grand.prototype.dto;

import java.io.*;

public class PrototypeObject1 implements Cloneable,Serializable{

    private String name;

    private String desc;

    private PrototypeObject2 prototypeObject2;

    public PrototypeObject1() { }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public PrototypeObject2 getPrototypeObject2() {
        return prototypeObject2;
    }

    public void setPrototypeObject2(PrototypeObject2 prototypeObject2) {
        this.prototypeObject2 = prototypeObject2;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        return deepClone();
    }

    private Object deepClone(){
        Object newObject = null;
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fos = new FileOutputStream("cloneTemp");
            oos = new ObjectOutputStream(fos);
            oos.writeObject(this);

            fis = new FileInputStream("cloneTemp");
            ois = new ObjectInputStream(fis);
            newObject = ois.readObject();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                if(ois!=null){
                    ois.close();
                }
                if(fis!=null){
                    fis.close();
                }
                if(oos!=null){
                    oos.close();
                }
                if(fos!=null){
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return newObject;
    }
}